package cn.golxen.rsms.config;


import cn.golxen.rsms.bean.entity.StoreMain;
import cn.golxen.rsms.commons.utils.YmlUtils;
import cn.golxen.rsms.service.StoreService;
import com.wechat.pay.java.core.Config;

import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.http.DefaultHttpClientBuilder;
import com.wechat.pay.java.core.http.HttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.UncheckedIOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Configuration
@Slf4j
public class WechatPayConfig {

    public static Map<Integer,Config> wechatPayConfigV3Map = new HashMap<>();
    public static Map<Integer,String> wechatPayConfigV2Map = new HashMap<>();
    /** 商户号 */
    public static String merchantId = "1636971735";
    /** 商户API私钥路径 */
    public static String privateKeyPath = YmlUtils.getString("wechat.private-key-path1");
    public static String privateKeyPath2 = YmlUtils.getString("wechat.private-key-path2");
    public static String privateKeyPath3 = YmlUtils.getString("wechat.private-key-path3");
    /** 商户证书序列号 */
    public static String merchantSerialNumber = YmlUtils.getString("wechat.merchant-serial-number");
    /** 商户APIV3密钥 */
    public static String apiV3key = YmlUtils.getString("wechat.apiV3-key");
//    @Bean("defaultConfig")
//    public Config getConfig(){
//        try {
//            // 设置下载自动更新证书时网络配置
//            DefaultHttpClientBuilder clientBuilder =
//                    new DefaultHttpClientBuilder()
//                            .proxy(new Proxy(Proxy.Type.HTTP,
//                                    new InetSocketAddress(YmlUtils.getString("wechat.proxy.host"),
//                                            YmlUtils.getInt("wechat.proxy.port"))));
//
//            // 设置商户配置，并使用 httpClientBuilder 设置 HttpClient 所需的网络配置
//            Config config =
//                    new RSAAutoCertificateConfig.Builder()
//                            .merchantId(merchantId)
//                            .privateKeyFromPath(privateKeyPath)
//                            .merchantSerialNumber(merchantSerialNumber)
//                            .apiV3Key(apiV3key)
//                            .build();
//            return config;
//        }catch (Exception e){
//            String privateKeyPath = YmlUtils.getString("wechat.private-key-path2");
//            // 设置下载自动更新证书时网络配置
//            DefaultHttpClientBuilder clientBuilder =
//                    new DefaultHttpClientBuilder()
//                            .proxy(new Proxy(Proxy.Type.HTTP,
//                                    new InetSocketAddress(YmlUtils.getString("wechat.proxy.host"),
//                                            YmlUtils.getInt("wechat.proxy.port"))));
//
//            // 设置商户配置，并使用 httpClientBuilder 设置 HttpClient 所需的网络配置
//            Config config =
//                    new RSAAutoCertificateConfig.Builder()
//                            .merchantId(merchantId)
//                            .privateKeyFromPath(privateKeyPath)
//                            .merchantSerialNumber(merchantSerialNumber)
//                            .apiV3Key(apiV3key)
//                            .build();
//            return config;
//        }
//
//    }

    public void reloadWechatPayConfig(List<StoreMain>storeMainList){

        for (StoreMain storeMain:storeMainList){
            try {
                Config config =
                        new RSAAutoCertificateConfig.Builder()
                                .merchantId(String.valueOf(storeMain.getStoreId()))
                                .privateKeyFromPath(privateKeyPath)
                                .merchantSerialNumber(merchantSerialNumber)
                                .apiV3Key(storeMain.getSecretV3())
                                .build();
                wechatPayConfigV3Map.put(storeMain.getStoreId(),config);
            } catch (UncheckedIOException e){
                try {
                    Config config =
                            new RSAAutoCertificateConfig.Builder()
                                    .merchantId(String.valueOf(storeMain.getStoreId()))
                                    .privateKeyFromPath(privateKeyPath2)
                                    .merchantSerialNumber(merchantSerialNumber)
                                    .apiV3Key(storeMain.getSecretV3())
                                    .build();
                    wechatPayConfigV3Map.put(storeMain.getStoreId(),config);
                }catch (UncheckedIOException e2){
                    try {
                        Config config =
                                new RSAAutoCertificateConfig.Builder()
                                        .merchantId(String.valueOf(storeMain.getStoreId()))
                                        .privateKeyFromPath(privateKeyPath3)
                                        .merchantSerialNumber(merchantSerialNumber)
                                        .apiV3Key(storeMain.getSecretV3())
                                        .build();
                        wechatPayConfigV3Map.put(storeMain.getStoreId(),config);
                    }catch (Exception e3){
                        log.info(storeMain.getStoreId()+"商户未入驻微信支付");
                    }
                }catch (Exception e2){
                    log.info(storeMain.getStoreId()+"商户未入驻微信支付");

                }

            }catch (Exception e){
                log.info(storeMain.getStoreId()+"商户未入驻微信支付");

            }

            wechatPayConfigV2Map.put(storeMain.getStoreId(),storeMain.getSecretV2());
        }
    }
}
